package com.common.www.controller.project;

import com.alibaba.fastjson.JSON;
import com.common.www.aop.resolver.CurrentUser;
import org.springframework.boot.base.jpa.service.IBaseService;
import com.common.www.bean.entity.Result;
import com.common.www.bean.entity.project.*;
import com.common.www.bean.vo.UserVo;
import com.common.www.controller.base.DefaultCrudController;
import com.common.www.data.NetCode;
import com.common.www.service.auth.UserService;
import com.common.www.service.project.*;
import com.common.www.utils.ParamsUtils;
import com.common.www.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin()
@Transactional
@Api(tags = "MGT 消防工程相关接口")
@RequestMapping(value = {"/mgt/xfgcBasic/"})
public class MGTXfgcBasicController extends DefaultCrudController<XfgcBasic, Long> {

    @Autowired
    private ProjectService projectService;
    @Autowired
    private UserService userService;
    @Autowired
    private XfgcBasicService xfgcBasicService;
    @Autowired
    private DiXiaShiService diXiaShiService;
    @Autowired
    private GaoCengService gaoCengService;
    @Autowired
    private XiaoGaoCengService xiaoGaoCengService;
    @Autowired
    private YangFangService yangFangService;
    @Autowired
    private BieShuService bieShuService;
    @Autowired
    private ShangYeService shangYeService;
    @Autowired
    private GongYuService gongYuService;
    @Autowired
    private YouErYuanService youErYuanService;

    @Override
    protected IBaseService getService() {
        return projectService;
    }

    @ApiOperation("添加的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "json", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/saveByJson", method = RequestMethod.POST)
    public Result saveByJson(String json,@CurrentUser UserVo userVo) {
        XfgcBasic basic = JSON.parseObject(json, XfgcBasic.class);
        DiXiaShi diXiaShi = diXiaShiService.saveInfo(basic.getDiXiaShi());
        GaoCeng gaoCeng = gaoCengService.saveInfo(basic.getGaoCeng());
        XiaoGaoCeng xiaoGaoCeng = xiaoGaoCengService.saveInfo(basic.getXiaoGaoCeng());
        YangFang yangFang = yangFangService.saveInfo(basic.getYangFang());
        BieShu bieShu = bieShuService.saveInfo(basic.getBieShu());
        ShangYe shangYe = shangYeService.saveInfo(basic.getShangYe());
        GongYu gongYu = gongYuService.saveInfo(basic.getGongYu());
        YouErYuan youErYuan = youErYuanService.saveInfo(basic.getYouErYuan());

        basic.setDiXiaShi(diXiaShi);
        basic.setGaoCeng(gaoCeng);
        basic.setXiaoGaoCeng(xiaoGaoCeng);
        basic.setYangFang(yangFang);
        basic.setBieShu(bieShu);
        basic.setShangYe(shangYe);
        basic.setGongYu(gongYu);
        basic.setYouErYuan(youErYuan);
        if (basic.getId() == null) {
            xfgcBasicService.saveAndFlush(basic);
        } else {
            // 如果 id 不为空，则从数据库中获取现有对象
            XfgcBasic existing = xfgcBasicService.getByIdLogic(basic.getId());
            if (existing != null) {
                // 如果存在，则更新现有对象的属性
                BeanUtils.copyProperties(basic, existing, "id");
                // 然后保存更新后的对象
                xfgcBasicService.saveAndFlush(existing);
            } else {
                // 如果不存在，则保存解析出来的对象
                xfgcBasicService.saveAndFlush(basic);
            }
        }
        return R.ok(basic);
    }


    @ApiOperation(value = "批量删除", notes = "根据ID集批量删除，返回true表示删除成功")
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "long")
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    public Result batchDelete(Long[] ids) {
        try {
            if (ParamsUtils.isInputEmpty(ids)) {
                return R.returnCode(NetCode.CODE_INPUT_OMIT);
            }
            for (int i = 0; i < ids.length; i++) {
                XfgcBasic basic = xfgcBasicService.findOne(ids[i]);
                basic.setFlag(0);
                xfgcBasicService.save(basic);
            }
            return R.ok(null, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("删除失败");
        }
    }


    @ApiOperation(value = "查询项目的消防工程配置信息", nickname = "", notes = "")
    @ApiImplicitParams(value = {
    })
    @RequestMapping(value = "/getXfgcBasic", produces = {"application/json"}, method = RequestMethod.POST)
    public Result getGdyBasic(Long projectId) {
        XfgcBasic existing = new XfgcBasic();
        if (projectId != null){
            existing = xfgcBasicService.getRepository().findByFlagAndProjectId(1,projectId);
        }
        return R.ok(existing);
    }



}
